/**
 * Author: George
 * Description: a menu is a data provider which provide 
 * MenuSubject(food/drink) for system use, and also it
 * provide file IO of menu.txt with reading custom data. 
 * 
 */
package GoldenEagle;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

public class Menu {
	private ArrayList<MenuSubject> arrMenuSubjects;
	private float[] fTImeSlotDiscount;

	private static ArrayList<String> strStore;
	public Menu(MenuSubject[] arrMenuSubject) {

	}
	/**
	 * return a copy of MenuSubject. so this function allow modification 
	 * of MenuSubject itself and not affect to the original one.
	 * @param index 
	 * @return MenuSubject
	 */
	public MenuSubject getMenuSubject(int index)
	{
		return arrMenuSubjects.get(index);
	}
	/**
	 * get MenuSubject by name, 
	 * @param name
	 * the name of MenuSubject
	 * @return
	 * reutrn null if no such MenuSubject
	 */
	public MenuSubject getMenuSubject(String name){
		for(int i = 0;i<arrMenuSubjects.size();i++)
		{
			MenuSubject ms = arrMenuSubjects.get(i);
			if(ms.getName().equals(name))	return ms;
		}
		return null;
	}
	/**
	 * return the index of MenuSubject
	 * @param ms
	 * @return
	 *  index of MenuSubject
	 */
	public int getIndex(MenuSubject ms){
		return arrMenuSubjects.indexOf(ms);
	}
	/**
	 * get or display the data of menu
	 * @param andDisplay
	 * set true if you want display 
	 * @return
	 * the display content includes index,foodname and price.
	 */
	public String getMenuList(boolean andDisplay)
	{
		String list = "";
		for(int i = 0;i<arrMenuSubjects.size();i++){
			MenuSubject ms = arrMenuSubjects.get(i);
			list += Integer.toString(i) +". " + ms.getName() + " Price: " + ms.getPrice() + "$\n";
		}
		if(andDisplay){
			System.out.println(list);
		}
		return list;
	}
	
	/**
	 * init menu by specified txt file
	 * @param txtFile
	 * file instance of menu(txt file)
	 */
	public Menu(File txtFile)
	{
		//load string from file
		DataProvider.praseFromFile(txtFile);
		arrMenuSubjects = praseMenuSubject(DataProvider.getDataStore());
		
		//test load data OK!
		//System.out.println(arrMenuSubjects.get(2).getName());
	}
	//when fulfill this condition ,the group of data read end
	
	/**
	 * prase data from string array from txt file to ArrayList<MenuSubject>
	 * @param store
	 * string array of menu txt file
	 * @return
	 * a list of MenuSubject data
	 */
	private ArrayList<MenuSubject> praseMenuSubject(ArrayList<String> store)
	{
		//todo
		ArrayList<MenuSubject> array = new ArrayList<MenuSubject>();
		String tempString;
		
		//prase data
		for(int i = 0;i < store.size();i++)
		{
			tempString = store.get(i);
			if(tempString.equals("[Food]") || tempString.equals("[Drink]")){
				//load food data until dataEndCondition
				boolean isFoodData = tempString.equals("[Food]");
				//food start index
				int fdIdx;
				for(fdIdx = i+1;!DataProvider.dataEndCondition(fdIdx);fdIdx++)
				{
					tempString = store.get(fdIdx);
					String[] data = tempString.split(":");
					
					array.add(new MenuSubject(
							data[0],isFoodData ? MenuSubject.FoodType.Meal : MenuSubject.FoodType.Drink,
									Float.parseFloat(data[1])));
					
					
				}
				i=fdIdx;
			}
		}
		
		return array;
	}
	
	
	public float getTimeSlotDiscount(int timeSlot){
		return 0;		
	}
	public static String getDefaultMenuFilePath() {
		return System.getProperty("user.dir") + "\\menu_file\\menu_file.txt";
	}
	

}
